package com.logic.modular.gameCore.match.battle.service.impl;

import com.comment.pojo.domain.logic.domain.PlayerHero;
import com.comment.pojo.domain.logic.core.CoreSkillTargetType;
import com.logic.modular.gameCore.match.battle.mapper.CoreSkillTargetTypeMapper;
import com.logic.modular.gameCore.match.battle.service.CoreSkillTargetTypeService;
import lombok.extern.slf4j.Slf4j;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct;


/**
 * (CoreSkillTargetType)表服务实现类
 *
 * @author cxy
 * @since 2024-08-22 11:01:38
 */
@Slf4j
@Service
public class CoreSkillTargetTypeServiceImpl extends ServiceImpl<CoreSkillTargetTypeMapper, CoreSkillTargetType> implements CoreSkillTargetTypeService {
    Cache<String, Map<Integer,CoreSkillTargetType>> cache = Caffeine.newBuilder().build();
    Cache<String, List<PlayerHero>> lastTarget = Caffeine.newBuilder().build();

    @PostConstruct
    private void init() {
        CoreSkillTargetType initSelect = CoreSkillTargetType.builder().build();
        List<CoreSkillTargetType> list = this.list(new LambdaQueryWrapper<>(initSelect));
        log.info("开始缓存数据,数据量为:{}",list.size());
        cache.put(
                "coreSkillTargetType", 
                list.stream().collect(Collectors.toMap(CoreSkillTargetType::getId, CoreSkillTargetType -> CoreSkillTargetType))
        );
        log.info("数据缓存完成");
    }

    public List<CoreSkillTargetType> listCache() {
        Map<Integer,CoreSkillTargetType> values = cache.getIfPresent("coreSkillTargetType");
        if (Objects.isNull(values)) {
            throw new RuntimeException("数据为空");
        }
        return new ArrayList<>(values.values());
    }     
    
    public Map<Integer,CoreSkillTargetType> mapCache() {
        Map<Integer,CoreSkillTargetType> values = cache.getIfPresent("coreSkillTargetType");
        if (Objects.isNull(values)) {
            throw new RuntimeException("数据为空");
        }
        return values;
    }   
    
    public CoreSkillTargetType getCache(Integer id) {
        Map<Integer,CoreSkillTargetType> values = cache.getIfPresent("coreSkillTargetType");
        if (Objects.isNull(values)) {
            throw new RuntimeException("数据为空");
        }
        return values.get(id);
    }
}
